﻿#include "ctimespanintvalidator.h"
#include <QDebug>

namespace ss {


CTimeSpanIntValidator::CTimeSpanIntValidator(int bottom /* = MIN_LOCK_SPAN*/, int top/* = MAX_LOCK_SPAN */, QObject *parent /*= nullptr*/ ) :
    QIntValidator(bottom, top, parent)
{

}

void CTimeSpanIntValidator::setRange(int bottom, int top)
{
    QIntValidator::setRange(bottom, top);
}

QValidator::State CTimeSpanIntValidator::validate(QString &s, int &n) const
{
    if (s.isEmpty())
    {
        return QValidator::Intermediate;
    }
    // 这里判断超过上下限，变成无效的参数
    int intValue = s.toInt();
    if (intValue > top() || intValue < bottom())
    {
        return QIntValidator::Invalid;
    }

    return QIntValidator::validate(s, n);
}

void CTimeSpanIntValidator::fixup(QString &input) const
{
    qDebug() << __FUNCTION__ <<  ": fixup============input==============" << input;
    //这里要做个判断，如果超过最大值取最大值 ，低于最小值取最小值
    int intValue = input.toInt();
    if (intValue < bottom())
    {
        input = QString("%1").arg(bottom());
    }
    if (intValue > top())
    {
        input = QString("%1").arg(top());
    }
}



}
